var videoPlay = false;
var indexVideoCorrente = 0;
var borderRightBoxCap = 20;

// JavaScript Document
$(document).ready(function(){
	
	setPositionListElementVideoManifattura();
	
	$('.video_manifattura').on('click',function(){
		indexVideoCorrente = $(this).index();
	});
	
});

$(window).resize(function(){
	setPositionListElementVideoManifattura();
});

function setPositionListElementVideoManifattura()
{	
	
	/*
	if($(".block_page.manifattura").find('li.active').find('.item_slider_prod_gallery').css('display') != "none")
	{
		$(".block_page.manifattura").find('li.active').find('.item_slider_prod_gallery').css('display','none');
	}
	*/
	
	var widthElementOriginal = 260;
	var numCorrelati = parseInt($('.sub_wrap_correlati').find('.item_video_manifattura').length,10);
	
	var elementRiga = Math.floor((widthContentTemplate-borderRightBoxCap)/(widthElementOriginal));
	if(elementRiga>numCorrelati)
		elementRiga = numCorrelati;	
	
	var righe = Math.ceil(numCorrelati/elementRiga);
	//alert(elementRiga + " " + $('.contenr_banner_about_panerai').find('.banneritem').width() + " " + widthContentTemplate);

	var withElementAboutPanerai = parseInt($('.sub_wrap_correlati').find('.item_video_manifattura').width(),10)+1;
	var heightElementAboutPanerai = parseInt($('.sub_wrap_correlati').find('.item_video_manifattura').height(),10)+parseInt($('.sub_wrap_correlati').find('.item_video_manifattura').css('margin-top'),10);
	var currentWidthElementAboutPanerai = ((widthContentTemplate-borderRightBoxCap)/elementRiga)-1;
	currentWidthElementAboutPanerai= Math.floor(currentWidthElementAboutPanerai);
	if(window_width  < MIN_WIDTH)
	{
		$('.sub_wrap_correlati').find('.item_video_manifattura').width(widthElementOriginal-1);
		$('.sub_wrap_correlati').find('.item_video_manifattura').find('.over_manifattura_item').width(widthElementOriginal-(borderRightBoxCap)-1);
	}
	else if((elementRiga*withElementAboutPanerai) <  (widthContentTemplate-borderRightBoxCap) || (elementRiga*withElementAboutPanerai) >  (widthContentTemplate-20))
	{
		var newWidthElementAboutPanerai = ((widthContentTemplate-borderRightBoxCap)/elementRiga)-1;
		newWidthElementAboutPanerai = Math.floor(newWidthElementAboutPanerai);
		$('.sub_wrap_correlati').find('.item_video_manifattura').width(newWidthElementAboutPanerai);
		$('.sub_wrap_correlati').find('.item_video_manifattura_bkg').width(newWidthElementAboutPanerai);
		$('.sub_wrap_correlati').find('.item_video_manifattura .border').width(newWidthElementAboutPanerai-borderRightBoxCap);
		$('.sub_wrap_correlati').find('.item_video_manifattura').find('.over_manifattura_item').width(newWidthElementAboutPanerai-borderRightBoxCap);
		$('.sub_wrap_correlati').find('.text_item_video_manifattura').width(newWidthElementAboutPanerai-(borderRightBoxCap*2));
		
		$('.sub_wrap_correlati').find('.item_video_manifattura').find('.over_manifattura_item').width(newWidthElementAboutPanerai-(borderRightBoxCap));
	}
	
	if(parseInt($('.wrap_prod_correlati').height(),10) > 0)
	$('.wrap_prod_correlati').height(righe*heightElementAboutPanerai);
	
	setScroller();
}

function playVideo(video)
{
	//trackEventAnalytics('TAB_ACCESSORIES','click_accessory_<?=$this->items[0]->codAccessori?>','Tab accessory options <?=$tab->diametro?> mm');
	
	var video = video;
	var hrefPlayVideo = "javascript:playVideo('"+video+"')";
	//alert(hrefPlayVideo)
	$('.sub_wrap_correlati.manifattura .video_manifattura').each(function(){
		
		if($(this).attr('href') == hrefPlayVideo)
		{
			var titleChapterManifattura = $(this).find('.text_item_video_manifattura').html();
			dataLayer.push({"videoTitle": titleChapterManifattura,"event": "playVideo"});
		}
		
	});
	
	$('.item_slider_prod_gallery').css('display','none');
	$('.title_page_manifattura').css('display','none');
	$('.play_manifattura').css('display','none');
	
	if(navigator.platform.indexOf("iPad") != -1 || navigator.platform.indexOf("iPhone") != -1)
	{
		$('#video_manifattura').html('<video id="video_ipad" src="'+video+'" type="video/mp4" width='+widthContentTemplate+' height='+newheightSectionBlockTemplate+' controls="controls" />');
		document.getElementById('video_ipad').play();	
	}
	else
	{	
	
		var data = new Date();
		var date = data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds() + ":" + data.getMilliseconds();
		
		if (swfobject.hasFlashPlayerVersion("10.0.0")) 
		{	
			var flashvars = {};
			flashvars.autoPlay = "1";
			flashvars.video = video;
			flashvars.loop = "0";
			flashvars.hideControls = "0";
	
			var params = {};
			params.menu = "false";
			params.quality = "high";
			params.wmode = "transparent";
			params.align = "top";
			params.allowscriptaccess = "sameDomain";
			params.allowfullscreen = "true";
			var attributes = {};
			//attributes.id = "flash_gallery_video_swf_header";
			//attributes.name = "flash_gallery_video_swf_header";	
			swfobject.embedSWF(base_url+"swf/AdactoVideoPlayer.swf?d="+date,'video_manifattura', widthContentTemplate, newheightSectionBlockTemplate, "10.0.0", false, flashvars, params, attributes);
			//setScroller();
			//$('#video_manifattura').css('display','inline');
			//$('object#1').css('display','inline');
		}
	}			
	
}

function changeStatusVideo()
{
	if(videoPlay)
	{
		$('.bt_find_my_panerai').show();
		$('.mod-languages').show();
		//alert(indexVideoCorrente + " == " +($('.video_manifattura').length-1))
		if(indexVideoCorrente == ($('.video_manifattura').length))
		{
			$('.item_slider_prod_gallery').css('display','inline');
			$('.title_page_manifattura').css('display','inline');
			$('.play_manifattura').css('display','inline');
			indexVideoCorrente = 0;
		}
		else
		{
			//indexVideoCorrente = indexVideoCorrente++;
			
			var valueHref = $('.video_manifattura').eq(indexVideoCorrente).attr('href');
			valueHref = valueHref.replace("javascript:playVideo('","").replace("')","");
			playVideo(valueHref);
		}
		videoPlay = false;
	}
	else
	{
		$('.bt_find_my_panerai').hide();
		$('.mod-languages').hide();
		trackEventAnalytics('MANUFACTURE_VIDEOS','play_video_manufacture','CAP_'+(indexVideoCorrente+1));
		indexVideoCorrente = indexVideoCorrente+1;
		
		videoPlay = true;
	}
}